#!/bin/bash

set -o errexit
set -o xtrace

test_dir=$(realpath $(dirname $0))
. ${test_dir}/../functions

CLUSTER='some-name'

main() {
	create_namespace $namespace
    deploy_operator

    kubectl_bin apply -f "${conf_dir}/client.yml" \
                      -f "${conf_dir}/secrets.yml"

    # test 1.0.0
    API="psmdb.percona.com/v1"
    apply_cluster "$test_dir/conf/${CLUSTER}-rs0.yml"
    wait_for_running "${CLUSTER}-rs0" "1" "false"

    compare_kubectl service/${CLUSTER}-rs0 "-100"
    compare_kubectl statefulset/${CLUSTER}-rs0 "-100"

    # test 1.1.0
    API="psmdb.percona.com/v1-1-0"
    kubectl_bin patch psmdb "${CLUSTER}" --type=merge --patch '{
        "spec": {"crVersion":"1.1.0"}
    }'
    wait_for_running "${CLUSTER}-rs0" "1" "false"

    compare_kubectl service/${CLUSTER}-rs0 "-110"
    compare_kubectl statefulset/${CLUSTER}-rs0 "-110"

    # test 1.2.0
    API="psmdb.percona.com/v1-2-0"
    kubectl_bin patch psmdb "${CLUSTER}" --type=merge --patch '{
        "spec": {"crVersion":"1.2.0"}
    }'
    wait_for_running "${CLUSTER}-rs0" "1" "false"

    compare_kubectl service/${CLUSTER}-rs0 "-120"
    compare_kubectl statefulset/${CLUSTER}-rs0 "-120"

    # test 1.3.0
    API="psmdb.percona.com/v1-3-0"
    kubectl_bin patch psmdb "${CLUSTER}" --type=merge --patch '{
        "spec": {"crVersion":"1.3.0"}
    }'
    wait_for_running "${CLUSTER}-rs0" "1" "false"

    compare_kubectl service/${CLUSTER}-rs0 "-130"
    compare_kubectl statefulset/${CLUSTER}-rs0 "-130"

    # test 1.4.0
    API="psmdb.percona.com/v1-4-0"
    kubectl_bin patch psmdb "${CLUSTER}" --type=merge --patch '{
        "spec": {"crVersion":"1.4.0"}
    }'
    wait_for_running "${CLUSTER}-rs0" "1" "false"

    compare_kubectl service/${CLUSTER}-rs0 "-140"
    compare_kubectl statefulset/${CLUSTER}-rs0 "-140"

    # test 1.5.0
    API="psmdb.percona.com/v1-5-0"
    kubectl_bin patch psmdb "${CLUSTER}" --type=merge --patch '{
        "spec": {"crVersion":"1.5.0"}
    }'
    wait_for_running "${CLUSTER}-rs0" "1" "false"

    compare_kubectl service/${CLUSTER}-rs0 "-150"
    compare_kubectl statefulset/${CLUSTER}-rs0 "-150"

    # test 1.6.0
    API="psmdb.percona.com/v1-6-0"
    kubectl_bin patch psmdb "${CLUSTER}" --type=merge --patch '{
        "spec": {"crVersion":"1.6.0"}
    }'
    wait_for_running "${CLUSTER}-rs0" "1" "false"

    compare_kubectl service/${CLUSTER}-rs0 "-160"
    compare_kubectl statefulset/${CLUSTER}-rs0 "-160"

    # test 1.7.0
    API="psmdb.percona.com/v1-7-0"
    kubectl_bin patch psmdb "${CLUSTER}" --type=merge --patch '{
        "spec": {"crVersion":"1.7.0"}
    }'
    wait_for_running "${CLUSTER}-rs0" "1" "false"

    compare_kubectl service/${CLUSTER}-rs0 "-170"
    compare_kubectl statefulset/${CLUSTER}-rs0 "-170"

    destroy $namespace
}

main
